package demo2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Fage {

    public static String getHtmlResourceByUrl(String urlObj, String encoding){
        InputStreamReader isr = null;
        BufferedReader bf = null;
        StringBuffer sb = new StringBuffer();
        try {
            //建立网络接连  异常机制
            URL url = new URL(urlObj);
            //打开网络连接
            URLConnection uc = url.openConnection();
            //下载 IO流 输入 输出 相对的
            //建立管道
            isr = new InputStreamReader(uc.getInputStream(),encoding);
            //缓冲
            bf = new BufferedReader(isr);

            //创建一个临时文件
            String temp = "";
            //拿到网站的所有内容
            while((temp=bf.readLine()) != null){
                sb.append(temp+"\n");
            }

        } catch (RuntimeException e) {//MalformedURLException e
            System.out.println("对不起,你的网络连接失败,请稍后重试");
            e.printStackTrace();
        }catch (IOException e) {
            System.out.println("世界最遥远的距离就是你在访问我，而我不见了");
            e.printStackTrace();
        }finally{
            try {
                //java 作用域 影响的范围   葱花
                if(bf!=null){
                    bf.close();
                }
                if(isr!=null){
                    isr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        // 评论地址： https://movie.douban.com/subject/26662193/comments?start=200&limit=20&sort=new_score&status=P&percent_type=
        int start = 0;
        while (start >= 0 && start <= 200) {
            String url = "https://movie.douban.com/subject/26662193/comments?start=" + start + "&limit=" + start
                    + "&sort=new_score&status=P&percent_type=";
            start = start + 20;
            String encoding = "utf-8";
            String html = getHtmlResourceByUrl(url, encoding);
            // 解析源代码,批量的采集我们需要的影评数据
            Document document = (Document) Jsoup.parse(html);
            // 拿着包裹影评的盒子id="comments";
            Element element = document.getElementById("comments");
            // 拿到comment-item 获取每一组评论
            Elements elements = document.getElementsByClass("comment-item");
            // 遍历所有的评论组
            for (Element el : elements) {
                String name = el.getElementsByTag("a").last().text();
                String desc = el.getElementsByTag("p").text();
                String time = el.getElementsByClass("comment-time").text();
                System.out.println("用户名:  " + name + "  评论:  " + desc + "  时间:  " + time);
            }
        }
    }

}
